import requests
TOKEN=''
def login():
    url = "https://api-ifs.oymgcx.com:40443/personnel/sync/api/sso/login"
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }
    data = {
        "userName": "wu1234",
        "password": "T521023w"
    }
    response = requests.request(
        method="POST",  # 请求方法
        url=url,        # 请求 URL
        headers=headers,  # 请求头
        data=data       # 请求体数据
    )
    return response.json()['data']



def make_request(method, url, data={}):
    headers = {
    }
    headers['authorization'] = TOKEN
    try:
        response = requests.request(method, url, json=data, headers=headers)
        if response.status_code == 200:
            return response.json()['data']  # 假设响应内容为 JSON 格式
        else:
            return f"{method} request failed with status code {response.status_code}"
    except requests.exceptions.RequestException as e:
        return f"{method} request failed: {e}"

# 示例调用
baseOrderList = lambda data:make_request("POST", "http://ifs.oylianjin.com:8846/api/its/order/listPage", data)
syncOrder = lambda data:make_request("POST", "http://ifs.oylianjin.com:8846/api/its/order/modifySyncStatus", data)


def startSync():
    global TOKEN
    res = login()
    TOKEN = res['token']
    inList = baseOrderList({
        "arrDateEnd": "2024-11-30T23:59:59",
        "arrDateStart": "2024-11-01T00:00:00",
        "current": 1,
        "orderStatus": 9,
        "orderType": 1,
        "size": 999,
        "syncStatus": 3
    })["records"]
    print(len(inList))
    outList = baseOrderList({
        "arrDateEnd": "2024-11-30T23:59:59",
        "arrDateStart": "2024-11-01T00:00:00",
        "current": 1,
        "orderStatus": 9,
        "orderType": 2,
        "size": 999,
        "syncStatus": 3
    })["records"]
    print(len(outList))
    success = True
    for item in inList+outList:
        syncres = syncOrder({
            "orderId":item["id"],
            "syncStatus": 0
        })
        if syncres is not None:
            success = False
            break
    if success:
        print('同步成功')
    else:
        print('同步失败请检查')
# startSync()